package com.example.a2022application;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.a2022application.activity.AddTeacherActivity;
import com.example.a2022application.activity.CorrectTaskActivity;
import com.example.a2022application.activity.PieChartActivity;
import com.example.a2022application.activity.SendTaskActivity;
import com.example.a2022application.bean.TeacherBean;
import com.example.a2022application.bean.TeacherTaskBean;
import com.example.a2022application.net.Urls;
import com.example.a2022application.utils.SpUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class login_teachers extends BaseActivity {
    private Activity myActivity;
    private ListView listtimetable;

    ShowListAdapter adapter;

    List<TeacherTaskBean.DataBean> tList= new ArrayList();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teachers_login);
        listtimetable = findViewById(R.id.listtimetable);
        myActivity = this;
        initData();

    }

    private void initData() {
        String uid = SpUtils.getString(this,"userid");
        OkGo.<String>post(Urls.SELECTTEACHERTASK)
                .params("userid",uid)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        TeacherTaskBean bean = getGson().fromJson(response.body(),TeacherTaskBean.class);
                        if (bean.getCode() == 0){
                            if (bean.getData().size()>0){
                                tList = bean.getData();
                                adapter = new ShowListAdapter();
                                listtimetable.setAdapter(adapter);
                            }

                        }
                    }
                });

    }


    public class ShowListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return tList.size();
        }

        @Override
        public Object getItem(int i) {
            return tList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            login_teachers.ShowListAdapter.ViewHolder viewHolder;
            //判断界面是否有缓存
            if(convertView == null){
                viewHolder= new login_teachers.ShowListAdapter.ViewHolder();
                convertView= LinearLayout.inflate(parent.getContext(), R.layout.item_select, null);

                viewHolder.tv_task = convertView.findViewById(R.id.tv_task);
                viewHolder.tv_send = convertView.findViewById(R.id.tv_send);
                viewHolder.tv_correct = convertView.findViewById(R.id.tv_correct);
                viewHolder.tv_statistics = convertView.findViewById(R.id.tv_statistics);

                convertView.setTag(viewHolder);
            }else{
                viewHolder=(login_teachers.ShowListAdapter.ViewHolder)convertView.getTag();
            }
            TeacherTaskBean.DataBean teacher = tList.get(position);

            viewHolder.tv_task.setText(teacher.getCourse_name()+"("+teacher.getClassname()+")");
            viewHolder.tv_send.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(login_teachers.this, SendTaskActivity.class);
                    intent.putExtra("oid",teacher.getTimetable_id());
                    startActivity(intent);

                }
            });
            //批改作业
            viewHolder.tv_correct.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(login_teachers.this, CorrectTaskActivity.class);
                    intent.putExtra("tid",teacher.getTimetable_id());
                    startActivity(intent);
                }
            });
            viewHolder.tv_statistics.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(login_teachers.this, PieChartActivity.class);
                    intent.putExtra("tid",teacher.getTimetable_id());
                    startActivity(intent);
                }
            });

            return convertView;
        }

        private class ViewHolder{
            TextView tv_task;
            TextView tv_send;
            TextView tv_correct;
            TextView tv_statistics;

        }
    }

    //退出
    public void exit(View view){
        new AlertDialog.Builder(this)
                .setTitle("确定退出登录？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        SpUtils.clear(login_teachers.this);
                        startActivity(new Intent(login_teachers.this, MainActivity.class));
                        finish();
                    }

                })
                .setNegativeButton("取消", null)
                .show();
    }
}